import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class AddFruitActivity extends AppCompatActivity {

    private EditText fruitNameEditText;
    private EditText fruitPriceEditText;
    private EditText fruitDescriptionEditText;
    private RadioGroup fruitFreshnessRadioGroup;
    private RadioButton freshRadioButton;
    private RadioButton semiFreshRadioButton;
    private RadioButton notFreshRadioButton;
    private Spinner fruitCategorySpinner;
    private Button addFruitButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_fruit);

        fruitNameEditText = findViewById(R.id.fruit_name_edit_text);
        fruitPriceEditText = findViewById(R.id.fruit_price_edit_text);
        fruitDescriptionEditText = findViewById(R.id.fruit_description_edit_text);
        fruitFreshnessRadioGroup = findViewById(R.id.fruit_freshness_radio_group);
        freshRadioButton = findViewById(R.id.fresh_radio_button);
        semiFreshRadioButton = findViewById(R.id.semi_fresh_radio_button);
        notFreshRadioButton = findViewById(R.id.not_fresh_radio_button);
        fruitCategorySpinner = findViewById(R.id.fruit_category_spinner);
        addFruitButton = findViewById(R.id.add_fruit_button);

        addFruitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fruitName = fruitNameEditText.getText().toString();
                String fruitPrice = fruitPriceEditText.getText().toString();
                String fruitDescription = fruitDescriptionEditText.getText().toString();

                int selectedFreshnessId = fruitFreshnessRadioGroup.getCheckedRadioButtonId();
                boolean isFresh = selectedFreshnessId == R.id.fresh_radio_button;
                boolean isSemiFresh = selectedFreshnessId == R.id.semi_fresh_radio_button;
                boolean isNotFresh = selectedFreshnessId == R.id.not_fresh_radio_button;

                String selectedCategory = fruitCategorySpinner.getSelectedItem().toString();

                if (fruitName.isEmpty() || fruitPrice.isEmpty() || fruitDescription.isEmpty() || selectedCategory.isEmpty()) {
                    Toast.makeText(AddFruitActivity.this, "水果名称、价格、描述和类别不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 在这里可以执行更复杂的添加水果逻辑
                // 例如，将水果信息保存到数据库、与服务器进行交互等

                Toast.makeText(AddFruitActivity.this, "水果添加成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}